/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MultiThread;

import heartnew.Board;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Oho
 */
public class ReadWrite extends Thread {

    ObjectInputStream inp;
    ObjectOutputStream out;
    ArrayList<ReadWrite> arrThread;
    boolean flag;
    int idplayer;

    public ReadWrite(ObjectInputStream inp, ObjectOutputStream out, ArrayList<ReadWrite> arrThread, boolean flag, int idplayer) {
        this.inp = inp;
        this.out = out;
        this.idplayer = idplayer;
        this.arrThread = arrThread;
        arrThread.add(this);
        this.flag = flag;
    }
    Board b;
    int iboard;//van thu i 

    public void Read() throws IOException, ClassNotFoundException {
        Object op = inp.readObject();

        if (op.equals("chiabai") == true) {
            iboard = (int) inp.readObject();
            if (iboard == 0) {
                b = new Board();


            } else {
                b.Init();//tao lai danh sach la bai cua tung nguoi choi dong thoi random la bai cua nguoi choi
                b.RandomCard();//random lai la bai 
                b.InitCardPlayer();//khoi tao lai danh sach bai cua nguoi choi thu i 
            }
            for (int i = 0; i < arrThread.size(); i++) {
//                b.getListplayer().get(i).getCardinhand().clear();
//                Object ob = b.getListplayer().get(i).getCardinhand();
                arrThread.get(i).Write("chiabai");
                arrThread.get(i).Write(b.getListplayer().get(i).getCardinhand());
                arrThread.get(i).Write(b.getBoard());//xac dinh nguoi duoc quyen danh quan bai la nguoi nao trong danh sach nguoi choi va goi id do cho client

                //chuyen du lieu sang client la nguoi duoc quyen danh quan bai do 
            }
        }
        if (op.equals("danhquanbai") == true) {
            Object vt = inp.readObject();//vi tri danh quan bai
            int idplayer = (int) inp.readObject();//id nguoi danh quan bai 
            int idnext = (idplayer +1)%4;
            Object card = inp.readObject();

            for (int i = 0; i < arrThread.size(); i++) {
                if (idplayer != i) {

                    arrThread.get(i).Write("danhquanbai");
                    arrThread.get(i).Write(vt);
                    arrThread.get(i).Write(idplayer);
                    arrThread.get(i).Write(card);
                    arrThread.get(i).Write(idnext);
                }

            }
        }
        //doc tung dong phia client goi ve server
        //neu chia bai ... 
        //new danh bai ...
        //neu danh voi may /...



    }

    public void Write(Object ob) throws IOException {
        //viet phuoong thuc write trong nay 
        //tuong ung voi chuc nang 
        out.writeObject(ob);
    }

    @Override
    public void run() {
        try {
            Write(flag);
            Write(0);
            Write(idplayer);
            flag = false;

            while (true) {
                try {
                    Read();
                } catch (IOException ex) {
                    try {
                        inp.close();
                        out.close();
                        arrThread.remove(this);
                        JOptionPane.showMessageDialog(null, ex);
                        return;
                    } catch (IOException ex1) {
                        Logger.getLogger(ReadWrite.class.getName()).log(Level.SEVERE, null, ex1);
                    }

                } catch (ClassNotFoundException ex) {
                    try {
                        inp.close();
                        out.close();
                        arrThread.remove(this);
                        return;

                    } catch (IOException ex1) {
                        Logger.getLogger(ReadWrite.class.getName()).log(Level.SEVERE, null, ex1);
                    }

                }
            }
        } catch (IOException ex) {
            try {
                inp.close();
                out.close();
                return;
            } catch (IOException ex1) {
                Logger.getLogger(ReadWrite.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }
}
